<?php
/**
 * @package Green Realty component
 * @copyright (C) 2012 Greenkey studio. All rights reserved
 * @license GNU/GPL Version 2 - http://www.gnu.org/licenses/gpl-2.0.html
 * @author Greenkey studio
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla view library
jimport('joomla.application.component.view');

/**
 * GRealty View
 */
class GRealtyViewGRealty extends JView
{
	/**
	 * View form
	 *
	 * @var		form
     * @var     canDo
	 */
	protected $form = null;
    protected $canDo = null;

    /**
	 * display method of Green Realty view
	 * @return void
	 */
	public function display($tpl = null)
	{
		// get the Data
		$form = $this->get('Form');
		$item = $this->get('Item');
        $script = $this->get('Script');

		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			JError::raiseError(500, implode('<br />', $errors));
			return false;
		}
		// Assign the Data
		$this->form = $form;
		$this->item = $item;
        $this->script = $script;

        /**
         * Save Higher permissions for param
         *@todo  Here we must read asset permission from DB for current param!!!!!!!!!!
         * This feature not realised, cause not need
         * When we do that, we must add new field - assetid, where we store id of _asset table
         */
        $canDo = GRealtyHelper::getActions($this->item->id);
        $this->canDo = $canDo;


		// Set the toolbar
		$this->addToolBar();

		// Display the template
		parent::display($tpl);

        // Set the document
		$this->setDocument();
	}

	/**
	 * Setting the toolbar
	 */
	protected function addToolBar()
	{
		$input = JFactory::getApplication()->input;
		$input->set('hidemainmenu', true);
        $user = JFactory::getUser();
		$userId = $user->id;
        $canDo = GRealtyHelper::getActions($this->item->id);
		$isNew = ($this->item->id == 0);

		JToolBarHelper::title($isNew ? JText::_('COM_GREALTY_MANAGER_NEW')
		                             : JText::_('COM_GREALTY_MANAGER_EDIT'));
        if ($isNew)
		{
			// For new records, check the create permission.
			if ($canDo->get('core.create'))
			{
				JToolBarHelper::apply('grealty.apply', 'JTOOLBAR_APPLY');
				JToolBarHelper::save('grealty.save', 'JTOOLBAR_SAVE');
				JToolBarHelper::custom('grealty.save2new', 'save-new.png', 'save-new_f2.png',
				                       'JTOOLBAR_SAVE_AND_NEW', false);
			}
			JToolBarHelper::cancel('grealty.cancel', 'JTOOLBAR_CANCEL');
		}
		else
		{
			if ($canDo->get('core.edit'))
			{
				// We can save the new record
				JToolBarHelper::apply('grealty.apply', 'JTOOLBAR_APPLY');
				JToolBarHelper::save('grealty.save', 'JTOOLBAR_SAVE');

				// We can save this record, but check the create permission to see
				// if we can return to make a new one.
				if ($canDo->get('core.create'))
				{
					JToolBarHelper::custom('grealty.save2new', 'save-new.png', 'save-new_f2.png',
					                       'JTOOLBAR_SAVE_AND_NEW', false);
				}
			}
			if ($canDo->get('core.create'))
			{
				JToolBarHelper::custom('grealty.save2copy', 'save-copy.png', 'save-copy_f2.png',
				                       'JTOOLBAR_SAVE_AS_COPY', false);
			}
			JToolBarHelper::cancel('grealty.cancel', 'JTOOLBAR_CLOSE');
		}
	}

    /**
	 * Method to set up the document properties
	 *
	 * @return void
	 */
	protected function setDocument()
	{
		$isNew = ($this->item->id == 0);
		//$document = JFactory::getDocument();
        // Replace this by JToolBar methods
		JToolBarHelper::title(JText::_($isNew ? 'COM_GREALTY_CREATING' : 'COM_GREALTY_EDITING'), 'grealty');

        $document = JFactory::getDocument();
        $document->addScript(JURI::root() . $this->script);
		$document->addScript(JURI::root() . "administrator/components/com_grealty"
		                                  . "/views/grealty/submitbutton.js");
		JText::script('COM_GREALTY_ERROR_UNACCEPTABLE');
    }
}

?>
